package com.jmb.apiplatform.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jmb.apiplatform.common.PageResult;
import com.jmb.apiplatform.exception.ValidException;
import com.jmb.apiplatform.mapper.DbMapper;
import com.jmb.apiplatform.pojo.DbDao;
import com.jmb.apiplatform.pojo.DbVO;
import com.jmb.apiplatform.service.DbService;
import com.jmb.apiplatform.util.JdbcUtil;
import com.jmb.apiplatform.util.ValidUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DbServiceImpl implements DbService {
    @Autowired
    private DbMapper dbMapper;

    @Override
    public void saveDb(DbDao dbDao) throws ValidException {
        String name = dbDao.getName();
        ValidUtil.notNUll(dbDao, "传入的对象不能为空");
        ValidUtil.notEmpty(name, "名称不能为空");
        ValidUtil.notNUll(dbDao.getType(), "数据库类型不能为空");
        ValidUtil.notEmpty(dbDao.getUrl(), "调试环境地址不能为空");
        ValidUtil.notEmpty(dbDao.getUsername(), "调试环境用户名不能为空");
        ValidUtil.notEmpty(dbDao.getPassword(), "调试环境密码不能为空");
        ValidUtil.notEmpty(dbDao.getDevUrl(), "开发环境地址不能为空");
        ValidUtil.notEmpty(dbDao.getDevUsername(), "开发环境用户名不能为空");
        ValidUtil.notEmpty(dbDao.getDevPassword(), "开发环境密码不能为空");
        ValidUtil.notEmpty(dbDao.getTestUrl(), "测试环境地址不能为空");
        ValidUtil.notEmpty(dbDao.getTestUsername(), "测试环境用户名不能为空");
        ValidUtil.notEmpty(dbDao.getTestPassword(), "测试环境密码不能为空");
        ValidUtil.notEmpty(dbDao.getProdUrl(), "预上线环境地址不能为空");
        ValidUtil.notEmpty(dbDao.getProdUsername(), "预上线环境用户名不能为空");
        ValidUtil.notEmpty(dbDao.getProdPassword(), "预上线环境密码不能为空");
        ValidUtil.notEmpty(dbDao.getProdUrl(), "线上环境地址不能为空");
        ValidUtil.notEmpty(dbDao.getProdUsername(), "线上环境用户名不能为空");
        ValidUtil.notEmpty(dbDao.getProdPassword(), "线上环境密码不能为空");
        ValidUtil.notNUll(dbDao.getStatus(), "状态不能为空");
        DbDao dbName = dbMapper.getDbList(name);
        if (dbName!=null){
            throw new ValidException("数据库名称已存在");
        }
        Date date = new Date();
        dbDao.setCreatedTime(date);
        dbDao.setUpdateTime(date);
        dbMapper.savaDb(dbDao);

    }

    @Override
    public DbDao getDbId(Integer id) throws ValidException {
        ValidUtil.notNUll(id, "id不能为空");
        DbDao dbDao = dbMapper.getDbId(id);
        if (dbDao == null) {
            throw new ValidException("id不存在,数据为空");
        }else {
            return dbDao;
        }
    }

    @Override
    public PageResult<DbDao> ListDao(DbVO dbVO) {
        log.info("分页查询传入参数的是:{}", JSON.toJSONString(dbVO));
        int page = dbVO.getPageNum() != null ? dbVO.getPageNum() : 1;
        int pageSize = dbVO.getPageSize() != null ? dbVO.getPageSize() : 10;
        PageHelper.startPage(page, pageSize);
        List<DbDao> daoList = dbMapper.ListDao(dbVO);
        Page<DbDao> p = (Page<DbDao>) daoList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void modifyDb(DbDao dbDao) throws ValidException {
        String dbName = dbDao.getName();
        DbDao name = dbMapper.getDbList(dbName);
        log.info("修改传入参数的是:{}", JSON.toJSONString(dbDao));
        if (name!=null && !name.getId().equals(dbDao.getId())){
            throw new ValidException("数据库名称已存在");
        }
        dbDao.setUpdateTime(new Date());
        dbMapper.modifyDb(dbDao);
    }

    @Override
    public void deleteDb(Integer id) throws ValidException {
        ValidUtil.notNUll(id, "id不能为空");
       dbMapper.getDbId(id);

    }

    @Override
    public String dbConnectInfo(Integer id) throws ValidException {
        DbDao dbVo = this.getDbId(id);
        String url = dbVo.getUrl();
        String username = dbVo.getUsername();
        String password = dbVo.getPassword();
        return JdbcUtil.checkJdbcConnection(url,username,password);
    }

    @Override
    public DbVO findDbById(Integer id) {
        return dbMapper.selectDbById(id);
    }
}
